<?php
/**
 * Created by PhpStorm.
 * User: Raytine
 * Date: 2018/8/8
 * Time: 11:01
 */
namespace PddWechat\Models\User\Service;
use Dai\Framework\Base\BaseService;
use Dai\Framework\Base\BaseSessionInfo;
use Dai\Framework\Library\CacheClass;
use PddWechat\Models\User\Data\UserData;

class IndirectMemberNum extends BaseService
{
    public function __construct()
    {
        $this->userdata = new UserData();
    }

    public function execute($basePageInfo)
    {
        /** @var BaseSessionInfo $sessionData */
        $sessionData = $basePageInfo->sessionInfo;
        $openid = $sessionData->openId;
        $tag = "UserOpenId_" . $openid;
        $num = CacheClass::cacheDataByRedis(__METHOD__, $tag, function () use ($openid) {
            return $this->getIndirectMemberNumber($openid);
        },300);
        return intval($num);
    }

    private function getIndirectMemberNumber($openid) {
        $select = "openId,userName";
        $res = $this->userdata->getMemberListByOpenId($openid,$select);
        $openids = [];
        foreach ($res as $item) {
            if(!empty($item['openId'])) {
                array_push($openids, $item['openId']);
            }
        }
        $openids = array_unique($openids);
        //获取间接团队人员数量
        $num = $this->userdata->getIndirectMemberNumByOpenId($openids);
        return $num;
    }
}